package com.yserp.kp.service.impl;
import com.yserp.kp.service.TKpOrderOneServiceI;
import com.yserp.kpyw.entity.TKpOrderOneEntity;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.io.Serializable;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter;

@Service("tKpOrderOneService")
@Transactional
public class TKpOrderOneServiceImpl extends CommonServiceImpl implements TKpOrderOneServiceI {

	
 	public void delete(TKpOrderOneEntity entity) throws Exception{
 		super.delete(entity);
 		//执行删除操作增强业务
		this.doDelBus(entity);
 	}
 	
 	public Serializable save(TKpOrderOneEntity entity) throws Exception{
 		Serializable t = super.save(entity);
 		//执行新增操作增强业务
 		this.doAddBus(entity);
 		return t;
 	}
 	
 	public void saveOrUpdate(TKpOrderOneEntity entity) throws Exception{
 		super.saveOrUpdate(entity);
 		//执行更新操作增强业务
 		this.doUpdateBus(entity);
 	}
 	
 	/**
	 * 新增操作增强业务
	 * @param t
	 * @return
	 */
	private void doAddBus(TKpOrderOneEntity t) throws Exception{
		//-----------------sql增强 start----------------------------
	 	//-----------------sql增强 end------------------------------
	 	
	 	//-----------------java增强 start---------------------------
	 	//-----------------java增强 end-----------------------------
 	}
 	/**
	 * 更新操作增强业务
	 * @param t
	 * @return
	 */
	private void doUpdateBus(TKpOrderOneEntity t) throws Exception{
		//-----------------sql增强 start----------------------------
	 	//-----------------sql增强 end------------------------------
	 	
	 	//-----------------java增强 start---------------------------
	 	//-----------------java增强 end-----------------------------
 	}
 	/**
	 * 删除操作增强业务
	 * @param id
	 * @return
	 */
	private void doDelBus(TKpOrderOneEntity t) throws Exception{
	    //-----------------sql增强 start----------------------------
	 	//-----------------sql增强 end------------------------------
	 	
	 	//-----------------java增强 start---------------------------
	 	//-----------------java增强 end-----------------------------
 	}
 	
 	private Map<String,Object> populationMap(TKpOrderOneEntity t){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", t.getId());
		map.put("create_name", t.getCreateName());
		map.put("create_by", t.getCreateBy());
		map.put("create_date", t.getCreateDate());
		map.put("update_name", t.getUpdateName());
		map.put("update_by", t.getUpdateBy());
		map.put("update_date", t.getUpdateDate());
		map.put("sys_org_code", t.getSysOrgCode());
		map.put("sys_company_code", t.getSysCompanyCode());
		map.put("bpm_status", t.getBpmStatus());
		map.put("doc_date", t.getDocDate());
		map.put("or_status", t.getOrStatus());
		map.put("order_type", t.getOrderType());
		map.put("cus_code", t.getCusCode());
		map.put("cus_name", t.getCusName());
		map.put("cus_type", t.getCusType());
		map.put("gd_code", t.getGdCode());
		map.put("gd_name", t.getGdName());
		map.put("cp_code", t.getCpCode());
		map.put("cp_name", t.getCpName());
		map.put("cp_guige", t.getCpGuige());
		map.put("cp_dengji", t.getCpDengji());
		map.put("cp_jifei", t.getCpJifei());
		map.put("or_qty", t.getOrQty());
		map.put("cp_jiage", t.getCpJiage());
		map.put("or_amount", t.getOrAmount());
		map.put("or_tuopan", t.getOrTuopan());
		map.put("or_remark", t.getOrRemark());
		map.put("by1", t.getBy1());
		map.put("by2", t.getBy2());
		map.put("by3", t.getBy3());
		map.put("by4", t.getBy4());
		map.put("by5", t.getBy5());
		map.put("by6", t.getBy6());
		return map;
	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @param t
	 * @return
	 */
 	public String replaceVal(String sql,TKpOrderOneEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{sys_org_code}",String.valueOf(t.getSysOrgCode()));
 		sql  = sql.replace("#{sys_company_code}",String.valueOf(t.getSysCompanyCode()));
 		sql  = sql.replace("#{bpm_status}",String.valueOf(t.getBpmStatus()));
 		sql  = sql.replace("#{doc_date}",String.valueOf(t.getDocDate()));
 		sql  = sql.replace("#{or_status}",String.valueOf(t.getOrStatus()));
 		sql  = sql.replace("#{order_type}",String.valueOf(t.getOrderType()));
 		sql  = sql.replace("#{cus_code}",String.valueOf(t.getCusCode()));
 		sql  = sql.replace("#{cus_name}",String.valueOf(t.getCusName()));
 		sql  = sql.replace("#{cus_type}",String.valueOf(t.getCusType()));
 		sql  = sql.replace("#{gd_code}",String.valueOf(t.getGdCode()));
 		sql  = sql.replace("#{gd_name}",String.valueOf(t.getGdName()));
 		sql  = sql.replace("#{cp_code}",String.valueOf(t.getCpCode()));
 		sql  = sql.replace("#{cp_name}",String.valueOf(t.getCpName()));
 		sql  = sql.replace("#{cp_guige}",String.valueOf(t.getCpGuige()));
 		sql  = sql.replace("#{cp_dengji}",String.valueOf(t.getCpDengji()));
 		sql  = sql.replace("#{cp_jifei}",String.valueOf(t.getCpJifei()));
 		sql  = sql.replace("#{or_qty}",String.valueOf(t.getOrQty()));
 		sql  = sql.replace("#{cp_jiage}",String.valueOf(t.getCpJiage()));
 		sql  = sql.replace("#{or_amount}",String.valueOf(t.getOrAmount()));
 		sql  = sql.replace("#{or_tuopan}",String.valueOf(t.getOrTuopan()));
 		sql  = sql.replace("#{or_remark}",String.valueOf(t.getOrRemark()));
 		sql  = sql.replace("#{by1}",String.valueOf(t.getBy1()));
 		sql  = sql.replace("#{by2}",String.valueOf(t.getBy2()));
 		sql  = sql.replace("#{by3}",String.valueOf(t.getBy3()));
 		sql  = sql.replace("#{by4}",String.valueOf(t.getBy4()));
 		sql  = sql.replace("#{by5}",String.valueOf(t.getBy5()));
 		sql  = sql.replace("#{by6}",String.valueOf(t.getBy6()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
 	
 	/**
	 * 执行JAVA增强
	 */
 	private void executeJavaExtend(String cgJavaType,String cgJavaValue,Map<String,Object> data) throws Exception {
 		if(StringUtil.isNotEmpty(cgJavaValue)){
			Object obj = null;
			try {
				if("class".equals(cgJavaType)){
					//因新增时已经校验了实例化是否可以成功，所以这块就不需要再做一次判断
					obj = MyClassLoader.getClassByScn(cgJavaValue).newInstance();
				}else if("spring".equals(cgJavaType)){
					obj = ApplicationContextUtil.getContext().getBean(cgJavaValue);
				}
				if(obj instanceof CgformEnhanceJavaInter){
					CgformEnhanceJavaInter javaInter = (CgformEnhanceJavaInter) obj;
					javaInter.execute("t_kp_order_one",data);
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("执行JAVA增强出现异常！");
			} 
		}
 	}
}